#include "ScriptPCH.h"
#include "Chat.h"

class vip_commandscript : public CommandScript
{
    public:
        vip_commandscript() : CommandScript("vip_commandscript") { }

        static bool HandleVIPTeleportCommand(ChatHandler* handler, const char* args)
        {
		char* tele_namee = strtok((char*)args, " ");
		if(!tele_namee)
		{
			handler->PSendSysMessage("Nutze .vip teleport #ORT , um dich an einen Ort zu teleportieren.");
			handler->PSendSysMessage("Nutze .vip listtele , um herauszufinden, welche Portpunkte dir zur Verfuegung stehen.");
			return true;
		}	
		else{
			std::string tele_name = tele_namee;
			uint32 accountId = handler->GetSession()->GetAccountId();
			QueryResult result_vip = LoginDatabase.PQuery("SELECT vip FROM account WHERE id = '%u'", accountId);
			Field *fieldw = result_vip->Fetch();
            uint32 check_vip = fieldw[0].GetUInt32();
			Player* me = handler->GetSession()->GetPlayer();
			GameTele const* tele = handler->extractGameTeleFromLink((char*)args);
			QueryResult result_tele_vip = WorldDatabase.PQuery("SELECT vip FROM game_tele WHERE name = '%s'", tele_name.c_str());
			if(result_tele_vip)
			{
			Field *fieldt = result_tele_vip->Fetch();
			uint32 tele_vip = fieldt[0].GetUInt32();

			if(check_vip == 1 && tele_vip == 1)
			{
			  me->TeleportTo(tele->mapId, tele->position_x, tele->position_y, tele->position_z, tele->orientation);	
			  return true;
			}
			else 
			{
			  handler->PSendSysMessage("Dein Account hat keinen VIP Status. Besuche www.MCWOW.net und erfahre mehr ueber VIPs.");
			  return true;
			}
			}
		}
        }
		

		
		static bool HandleVIPListTeleCommand(ChatHandler* handler, const char* args)
        {
		QueryResult resultListTele = WorldDatabase.PQuery("SELECT name FROM game_tele WHERE vip = '1'");
		do
        {
			Field* fieldsTeleName = resultListTele->Fetch();
			std::string tele_name = fieldsTeleName[0].GetString();
            if (resultListTele)
            {
                handler->PSendSysMessage(tele_name.c_str());
            }
            else
                handler->PSendSysMessage("Kein Tele gefunden");
        } while (resultListTele->NextRow());	
		return true;
        }
		
		
        ChatCommand* GetCommands() const
        {
			
		    static ChatCommand VIPSubCommandTable[] =
            {
                { "teleport", SEC_PLAYER, true, &HandleVIPTeleportCommand, "", NULL },
				{ "listtele", SEC_PLAYER, true, &HandleVIPListTeleCommand, "", NULL },
                { NULL, 0, false, NULL, "", NULL }
            };
			
		
            static ChatCommand VIPCommandTable[] =
            {
                { "vip", SEC_MODERATOR, true, NULL, "", VIPSubCommandTable  },
                { NULL, 0, false, NULL, "", NULL }
            };
            return VIPCommandTable;
        }
		
};

void AddSC_vip_commandscript()
{
    new vip_commandscript();
}